using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Windows.Forms;


namespace binary
{
    public class PolaczenieSql
    {
        
        public SqlConnection _polaczenie;
        public SqlCommand _cmd = new SqlCommand();

        public PolaczenieSql(string server, string baza)
        {
            //string _connectionString = @"Data Source=SDS03;Initial Catalog="+baza+";User Id=admin;Password=adminadmin;";
            string _connectionString = @"Data Source="+server+";Initial Catalog=" + baza + ";User Id=sa;Password=skareb!@#;";
            _polaczenie = new SqlConnection(_connectionString);
            try
            {
                _polaczenie.Open();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void Polacz()
        {
            try
            {
                _polaczenie.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public DataTable Zapytaj(string query)
        {
            SqlDataAdapter da = new SqlDataAdapter(query, _polaczenie);
            DataSet ds = new DataSet();
            da.Fill(ds, "wynik");
            DataTable dt = ds.Tables["wynik"];
            ds.Dispose();
            da.Dispose();
            return dt;
        }

        public void ZapytajVoid(string query)
        {
            try
            {
                SqlCommand cmd = new SqlCommand(query, this._polaczenie);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        
        public void Rozlacz()
        {
            _polaczenie.Close();
        }

        public void ladujDBF(string tabela, DataTable dt)
        {
            SqlDataAdapter ad = new SqlDataAdapter("select * from "+tabela, this._polaczenie);
            SqlCommandBuilder b = new SqlCommandBuilder(ad);
            ad.Fill(dt);
            ad.AcceptChangesDuringUpdate = false;
            ad.Update(dt);

        }


    }
}
